package api

import (
	"fmt"
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
)

// Enterprise 企业管理
type Enterprise struct {
	EnterpriseBll *bll.Enterprise `inject:""`
}

// QueryPage 查询分页数据
func (a *Enterprise) QueryPage(ctx *context.Context) error {
	params := schema.EnterpriseMobileQueryParam{
		LastID:   util.S(ctx.Query("lastID")).Int64(),
		Category: ctx.Query("category"),
		Name:     ctx.Query("name"),
		IsHot:    util.S(ctx.Query("isHot")).Int(),
	}

	items, err := a.EnterpriseBll.QueryMobilePage(params, ctx.GetPageSize())
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	return ctx.ResSuccess(items)
}

// QueryCategory 查询分类数据
func (a *Enterprise) QueryCategory(ctx *context.Context) error {
	items, err := a.EnterpriseBll.QueryCategory()
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	return ctx.ResSuccess(items)
}

// Get 获取企业详情
func (a *Enterprise) Get(ctx *context.Context) error {
	item, err := a.EnterpriseBll.Get(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	} else if item == nil {
		return ctx.ResErrBadRequest(fmt.Errorf("未找到企业数据"))
	}

	data := map[string]interface{}{
		"name":         item.Name,
		"contacter":    item.Contacter,
		"contact_tel":  item.ContactTel,
		"address":      item.Address,
		"logo":         item.Logo,
		"introduction": item.Introduction,
	}
	return ctx.ResSuccess(data)
}
